package com.example.yifei.ui.page.main.me


import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.annotation.ExperimentalCoilApi
import com.example.yifei.R
import com.example.yifei.ui.page.common.RouteName
import com.example.yifei.utils.RouteUtils

/**
 * @Author: zhudaihao
 * @Date: 2022/5/7
 * @Describe:我的
 */

@ExperimentalCoilApi
@Composable
fun MePage(
    navCtrl: NavHostController,
    viewModel: MeViewModel = hiltViewModel()
) {

    Column(
        modifier = Modifier
            .background(color = Color.White)
            .fillMaxWidth()
            .fillMaxHeight()
    ) {
        Box {
            Image(
                painter = painterResource(R.mipmap.bg_me),
                contentDescription = null,
                contentScale = ContentScale.FillBounds,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(202.dp)
            )


            Column {
                Title(viewModel)

                LazyColumn(modifier = Modifier.padding(bottom = 56.dp)) {
                    itemsIndexed(viewModel.list) { _, item ->
                        Item(item, onClick = {
                            RouteUtils.navTo(navCtrl = navCtrl, RouteName.LOGIN)
                        })
                    }
                }

            }


        }


    }
}

@ExperimentalCoilApi
@Composable
private fun Title(viewModel: MeViewModel) {
    Column(
        modifier = Modifier
            .padding(start = 20.dp, top = 60.dp, end = 16.dp, bottom = 20.dp)
    ) {
        Row(
            modifier = Modifier.padding(top = 24.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {

            Image(
                painter = painterResource(id = viewModel.image),
                contentDescription = "头像",
                modifier = Modifier
                    .size(64.dp)
                    .border(width = 1.dp, color = Color.White, shape = CircleShape)
            )

            Column(modifier = Modifier.padding(start = 16.dp)) {
                Text(
                    text = "13642718822",
                    fontSize = 24.sp,
                    color = Color(0xFF373C55)
                )

                Text(
                    modifier = Modifier.padding(top = 1.dp),
                    text = "欢迎使用易菲APP",
                    fontSize = 14.sp,
                    color = Color(0xFF9C9FB9)
                )
            }


        }
    }
}


@Composable
private fun Item(item: MeViewItemBean, onClick: () -> Unit) {
    Column(modifier = Modifier
        .clickable { onClick() }) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 20.dp, end = 20.dp)
                .height(54.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {

            Image(
                painter = painterResource(id = item.icon), contentDescription = "图标",
            )


            Row(
                modifier = Modifier
                    .fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween,
            ) {

                Text(
                    modifier = Modifier.padding(start = 10.dp),
                    text = item.name,
                    fontSize = 16.sp,
                    color = Color(0xff373C55)
                )


                Image(
                    painter = painterResource(id = R.mipmap.icon_arrow_right),
                    contentDescription = "图标",
                )

            }


        }


    }

    Box(
        modifier = Modifier
            .height(0.5.dp)
            .fillMaxWidth()
            .padding(start = 64.dp, end = 21.dp)
            .background(color = Color(0xffF0F0F0))
    )
}
 